package sdm.sensor.nasdaq;

import java.util.Calendar;
import java.util.Date;

import org.apache.log4j.ConsoleAppender;
import org.apache.log4j.Level;
import org.apache.log4j.Logger;
import org.apache.log4j.SimpleLayout;

import de.hdtconsulting.yahoo.finance.YHistoric;
import de.hdtconsulting.yahoo.finance.YQuote;
import de.hdtconsulting.yahoo.finance.YSymbol;
import de.hdtconsulting.yahoo.finance.Yapi;
import de.hdtconsulting.yahoo.finance.csv.format.YFormat;
import de.hdtconsulting.yahoo.finance.csv.format.YTag;

public class NasdaqSensor {
	
	public NasdaqSensor() {
	// init log4j
	SimpleLayout layout = new SimpleLayout();
	ConsoleAppender appender = new ConsoleAppender(layout);
	Logger.getRootLogger().addAppender(appender);
	Logger.getRootLogger().setLevel((Level) Level.FATAL);
	
	
	Yapi yapi = new Yapi();
	
	YFormat format = new YFormat();
	format.setStatusOn(YTag.BID);
	format.setStatusOn(YTag.BID_REAL_TIME);
	format.setStatusOn(YTag.NAME);
	
	yapi.setFormat(format);
	
	YSymbol symbol = new YSymbol("AAPL");
	yapi.addQuote(symbol);

	// works after first refresh!
//	System.out.println(yapi.getCsv());
//	System.out.println(yapi.getRefreshTime());

	yapi.refresh();
	
	System.out.println();
	System.out.println(yapi.getCsv());
	System.out.println(yapi.getRefreshTime());

	Calendar cal = Calendar.getInstance();
	cal.clear();
	cal.set(2011, 0, 1);
	Date startDate = cal.getTime();
	Date endDate;// = cal.getTime();

	
	for (int i = 0; i < 40; i++) {
		
	cal.add(cal.DAY_OF_MONTH, 1);
	endDate = cal.getTime();
	YQuote quote = yapi.getHistoric(symbol, startDate,endDate, Yapi.HIST_DAYLY);
	startDate = endDate;
	
	for (YHistoric historic: quote.getHistorics()) {
		System.out.println(historic.getClose());
	} 
	}
	}

}


